var dialog = new Object();

dialog.hide = function(callback) {
    $("#dialog-screen").fadeOut(500);
    
    $("#dialog").fadeOut(250);
    
    var post_left = (windowWidth() - dialog.width()) * 0.5;
    var post_top = (windowHeight() - dialog.height()) * 0.5;
    $("#dialog").css({
        top: post_top,
        left: post_left
    });
    
    if (callback) {
        setTimeout(
            function() {
                try {
                    callback();
                } catch (e) {
                    // Do nothing
                }
            },
            550
        );
    }
}

dialog.show = function(text, selfDestruction, showCloseButton, callback) {
    if (typeof(text) == "undefined") {
        text = '';
    }
    
    if (text == '') {
        $("#dialog-close").show();
    } else {
        if (showCloseButton) {
            $("#dialog-close").show();
            $("#general-message").css("margin-right", "20px");
        } else {
            $("#dialog-close").hide();
            $("#general-message").css("margin-right", "0");
        }
        $("#dialog .dialog-section").hide();
    }
    
    var doShowModalDialog = !text.match(/^\[opt:open-screen\]/i);
    
    text = text.replace(/^(\[[^\]]+\])+/, "");
    
    if (text.length > 0) {
        $("#dialog #general-message").html(text);
    } else {
        $("#dialog #general-message").empty();
    }
    
    // Option: Open screen (normal dialog)
    if (doShowModalDialog) {
        $("#dialog-screen").fadeIn(100, function() {
            $("#dialog-screen").fadeTo(100, 0.7);
        });
    }
    
    var post_left = (windowWidth() - dialog.width()) * 0.5;
    var post_top = (windowHeight() - dialog.height()) * 0.5;
    
    $("#dialog").css({
        top: post_top,
        left: post_left
    });
    
    $("#dialog").fadeIn(500);
    
    if (selfDestruction) {
        setTimeout(dialog.hide, 500);
    }
    
    if (callback) {
        setTimeout(callback, 550);
    }
}

dialog.autoPosition = function(callback) {
    var post_left = (windowWidth() - dialog.width()) * 0.5;
    var post_top = (windowHeight() - dialog.height()) * 0.5;
    $("#dialog:visible").css({
        top: post_top,
        left: post_left
    });
    
    try {
        if (callback != null && typeof(callback) != "undefined") {
            callback();
        }
    } catch (e) {
        // Do nothing
    }
}

dialog.height = function() {
    return parseInt($("#dialog").height());
}

dialog.width = function() {
    return parseInt($("#dialog").width())
}

dialog.showActionInMotion = function(action) {
    dialog.show(dictionary.locale.message.event.actionInProgress(action), false, false);
}